/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller.dashboard;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.MessageSenderLocal;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.SMSUtil;
import br.com.jr2tecnologia.model.CarrierPersistenceLocal;
import br.com.jr2tecnologia.model.ClientPhonePersistenceLocal;
import br.com.jr2tecnologia.model.LoginPersistenceLocal;
import br.com.jr2tecnologia.model.SmsReceivedPersistenceLocal;
import br.com.jr2tecnologia.model.SmsSendPersistenceLocal;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.SmsReceived;
import br.com.jr2tecnologia.model.entity.SmsSend;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.SelectItem;

/**
 *
 * @author ronildo
 */

@ManagedBean
@SessionScoped
public class SmsDashboardMBean implements Serializable{
    
    private static final Logger logger = LoggerFactory.getLogger();
    
    private final Login loginInSession;

    @EJB
    private MessageSenderLocal messageSender;
    @EJB
    private LoginPersistenceLocal loginPersistence;
    @EJB
    private SmsSendPersistenceLocal smsSendPersistence;
    @EJB
    private SmsReceivedPersistenceLocal smsReceivedPersistence;
    @EJB
    private ClientPhonePersistenceLocal clientPhonePersistence;
    @EJB
    private CarrierPersistenceLocal carrierPersistence;
    

    private String clientPhoneSelected;
    private String phone;
    private boolean sendTip;
    private SmsSend createSmsSend;
    private List<SmsSend> listSmsSend;
    private List<SmsReceived> listSmsReceived;
    private List<ClientPhone> listClientPhone;

    public SmsDashboardMBean() {
        loginInSession = SMSMailUtil.getLoginInSession();
        createSmsSend = new SmsSend();
    }

    @PostConstruct
    public void initialize(){
        searchSmsSend();
        searchSmsReceived();
    }

    public SmsSend getCreateSmsSend() {
        return createSmsSend;
    }

    public void setCreateSmsSend(SmsSend createSmsSend) {
        this.createSmsSend = createSmsSend;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public boolean isSendTip() {
        return sendTip;
    }

    public void setSendTip(boolean sendTip) {
        this.sendTip = sendTip;
    }

    public List<SmsSend> getListSmsSend() {
        return listSmsSend;
    }

    public List<SmsReceived> getListSmsReceived() {
        return listSmsReceived;
    }

    public String getClientPhoneSelected() {
        return clientPhoneSelected;
    }

    public void setClientPhoneSelected(String clientPhoneSelected) {
        this.clientPhoneSelected = clientPhoneSelected;
    }

    public List<SelectItem> getMenuClientPhone() {
        List<SelectItem> menuClientPhone = new ArrayList<SelectItem>();
        listClientPhone = clientPhonePersistence.list(new ClientPhone(loginInSession));

        menuClientPhone.add(new SelectItem(0, ""));
        for(int cont = 1, index = 0; index < listClientPhone.size(); cont++, index++){
            menuClientPhone.add(new SelectItem(cont, listClientPhone.get(index).getName()));
        }
        return menuClientPhone;
    }

    public void searchSmsSend(){
        listSmsSend = new LinkedList<SmsSend>();
        Login loginSearch = new Login();
        loginSearch.setCompany(loginInSession.getCompany());
        List<Login> loginsOfCompany = loginPersistence.list(loginSearch);

        for(Login login : loginsOfCompany){
            SmsSend smsSend = new SmsSend();
            smsSend.setLogin(login);
            listSmsSend.addAll(smsSendPersistence.list(smsSend));
        }
    }

    public void searchSmsReceived(){
        listSmsReceived = new LinkedList<SmsReceived>();
        Login loginSearch = new Login();
        loginSearch.setCompany(loginInSession.getCompany());
        List<Login> loginsOfCompany = loginPersistence.list(loginSearch);

        for(Login login : loginsOfCompany){
            SmsReceived smsReceived = new SmsReceived();
            smsReceived.setLogin(login);
            listSmsReceived.addAll(smsReceivedPersistence.list(smsReceived));
        }
    }

    public void sendSMS(){
        createSmsSend.setPkSmsSend(null);
        createSmsSend.setCarrier(carrierPersistence.getDefaultCarrier());
        createSmsSend.setLogin(loginInSession);

        if(sendTip){
            List<KeyResponse> listKeyResponse = loginPersistence.listAllKeyResponse(loginInSession);
            for(KeyResponse keyResponse :  listKeyResponse)
                if(keyResponse.getDefaultKey())
                    createSmsSend.setMessage(keyResponse.getTip()+createSmsSend.getMessage());
        }

        if(Integer.parseInt(clientPhoneSelected) > 0){
            createSmsSend.setClientPhone(listClientPhone.get(Integer.parseInt(clientPhoneSelected)-1));
            messageSender.persist(createSmsSend);
            SMSMailUtil.putFacesMessage("Sucesso","Sua mensagem esta sendo enviada");
            searchSmsSend();
        }else if(phone != null && !phone.equals("")){
            phone = SMSUtil.arrangesPhone(phone);
            if(phone != null)
                messageSender.persist(createSmsSend, phone);
            else
                SMSMailUtil.putFacesMessage("Erro","Telefone inválido");
        }else
            SMSMailUtil.putFacesMessage("Erro","Telefone inválido");
    }
    
    public void importSms(){
        searchSmsSend();
        SMSMailUtil.putFacesMessage("Sucesso","Importação realizada com sucesso");
    }
}
